/////////////////////////////////////////////////////////////
//  myGamesViewController.m                                //
//  myGames                                                //
//                                                         // 
//  Created by binbinming on 09-12-25.                     //
//  Copyright __MyCompanyName__ 2009. All rights reserved. //
/////////////////////////////////////////////////////////////

#import "myGamesViewController.h"
#import "MainGameViewController.h"
#import "ScorelistViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AudioToolbox/AudioQueue.h>
#import <AVFoundation/AVFoundation.h>

@implementation myGamesViewController

@synthesize playBtn;
@synthesize scoreBtn;
@synthesize aboutmeBtn;
@synthesize helpBtn;
@synthesize mainGameViewController;
@synthesize scorelistViewController;
@synthesize player;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	MainGameViewController *mvc = [[MainGameViewController alloc] initWithNibName:@"MainGameViewController" bundle:nil];
	self.mainGameViewController = mvc;
	[self playBackgroundSoundEffect];
}

-(void)startMusic {
	NSString *path = [[NSBundle mainBundle] pathForResource:@"backgroundMusic" ofType:@"wav"];
	SystemSoundID sid;
	AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &sid);
	AudioServicesPlaySystemSound(sid);
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


/**    IBAction implements for this viewController     **/

-(IBAction)startPlay:(id)sender{
	[self reCoderUserforLogin];
	//[self startMusic];
}

- (void) playBackgroundSoundEffect {
    NSString *soundFilePath = 
    [[NSBundle mainBundle] pathForResource: @"backgroundMusic" 
                                    ofType: @"wav"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
    AVAudioPlayer *newPlayer = 
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                           error: nil];
    [fileURL release];
    self.player = newPlayer; 
    [newPlayer release];
    [self.player prepareToPlay];
    [self.player setDelegate: self];
    self.player.numberOfLoops = -1;    // 循环播放音频，直到调用Stop方法
    [self.player play];
}

- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player successfully: (BOOL) flag { 
    if (player == self.player && flag == YES) { 
        NSLog(@"Playback finish.");
    } 
}

-(void)reCoderUserforLogin{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Input Your Name" message:@"First Input your Name :" 
											  delegate:self cancelButtonTitle:@"Cancel" 
										      otherButtonTitles:@"Ok",nil];
	UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 40, 200, 25)];
	textField.tag = 100;
	CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
	[alert setTransform:myTransform];
	[textField setBackgroundColor:[UIColor whiteColor]];
	[alert addSubview:textField];
	[alert show];
	[alert release];
}

/*-(void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
	UITextField *field = (UITextField *)[alert viewWithTag:100];
}
*/
-(IBAction)scoreDisplay:(id)sender{
	/*
	 UIAlertView *listView = [[UIAlertView alloc]  initWithTitle:@"About Me" message:nil
													   delegate:self
											  cancelButtonTitle:nil otherButtonTitles:nil];
	[listView addSubview:[[ScorelistViewController alloc]initWithNibName:@"ScorelistViewController" bundle:nil]];
	[listView show];*/
	
	//[listView release];
	
	scorelistViewController = [[ScorelistViewController alloc] initWithNibName:@"ScorelistViewController" bundle:nil];
	[self.view addSubview:self.scorelistViewController.view];
}

-(IBAction)aboutMeDisplay:(id)sender{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"About Me" message:@"Email:binbinming@gmail.com power by binbinming."
													   delegate:self 
											  cancelButtonTitle:nil otherButtonTitles:@"OK",nil]; // 一定在最后设置一个nil数值.
	[alertView show];
	[alertView release];
}

-(IBAction)helpDisplay:(id)sender{
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Game Help" message:@"Game Help :/n"
													   delegate:self 
											  cancelButtonTitle:nil otherButtonTitles:@"OK",nil]; // 一定在最后设置一个nil数值.
	[alertView show];
	[alertView release];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	//由于alertView 代理是self 所以 ok 处理在这里添加.
	NSLog(@"alertView to alert for me.");
	
	//如果输入名称或者检查本地已有用户名则开始切换到游戏界面.
	if(buttonIndex==1){
		[self.view addSubview:self.mainGameViewController.view];
	}
}

- (void)dealloc {
    [super dealloc];
}

@end
